<?php if (!defined('BASEPATH'))exit('No direct script access allowed');

class Site extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('users_log_model');
        $this->load->model('account_model');
        $this->load->model('new_task_model');
        $this->load->model('users_spread_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
        $this->load->model('users_log_model');
        $this->load->library('Email');
    }
    
    /**
     * 登录处理
     */
    function login(){
        
        if($_POST){
            
            $this->session->set_userdata("sms_code",null);
            if($this->userinfo){
                $mess['reqStatus'] = true;
                $mess['redirectURL'] = $this->redirectLink;
                echo JSON($mess);exit;
            }
            
            
            $loginName = isset($_POST['loginName']) ? trim($_POST['loginName']) : "" ;
            $loginPass = isset($_POST['loginPass']) ? trim($_POST['loginPass']) : "" ;
            $chkCapcha = isset($_POST['chkCapcha']) ? trim($_POST['chkCapcha']) : "" ;
            //检测用户名
            if(!$loginName){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = '登录用户名不能为空';
                echo JSON($mess);exit;
            }
            if(!$loginPass){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = '登录密码不能为空';
                echo JSON($mess);exit;
            }
            if(!$chkCapcha){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = '验证码不能为空';
                echo JSON($mess);exit;
            }
            
            
            $uinfo = "";
            if(check_email($loginName)){
                $uinfo = $this->users_model->get_user_by_email($loginName);
            }elseif(check_mobile($loginName)){
                $uinfo = $this->users_model->get_user_by_mobile($loginName);
            }else{
                $uinfo = $this->users_model->get_user_by_username($loginName);
            }
            if(!$uinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = '登录用户名输入有误';
                echo JSON($mess);exit;
            }
            
            //查看是否被锁定
            if($uinfo['status'] == 0){
                $mess['errorMsg'] = '对不起，您的账户处于锁定状态，禁止登录';
                $mess['errorCode'] = 1008;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            $ip = $this->input->ip_address();
            $is_pass = $uinfo['password'] == md5(md5($loginPass) . $uinfo['salt']);
            /**同盾接口防护start*/
            $fraud_config = $this->web_config['FRAUD'];
            $params = array(
                'event_id' => $fraud_config['login'],
                'token_id' => $this->session_id,
                'account_login' => $loginName,
                'ip_address' => $ip,
                'state' => !$is_pass ? 1 : 0,
            );
            $response = $this->fraud->init($params)->invoke_fraud_api();
            if(!$response['success']){
                $mess['errorMsg'] = $response['reason_code'];
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            /**同盾接口防护end*/
            //验证密码
            if(!$is_pass){
                $mess['errorMsg'] = '登录密码错误';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            
            if($chkCapcha != $this->session->userdata('capchaCode')){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = '验证码输入有误';
                echo JSON($mess);exit;
            }
            
            //登录成功，写入日志记录
            $log_data = array();
            $log_data['user_id'] = $uinfo['user_id'];
            $log_data['events'] = 'login';
            $log_data['content'] = '登录成功';
            $log_data['ip'] = $this->input->ip_address();
            $log_data['create_time'] = time();
            $this->users_log_model->insert($log_data);
            //更改用户信息
            $data = array();
            $data['login_times'] = $uinfo['login_times'] + 1;
            $data['last_ip'] = $uinfo['login_ip'];
            $data['last_time'] = $uinfo['login_time'];
            $data['login_ip'] = $this->input->ip_address();
            $data['login_time'] = time();
            $this->users_model->user_update($data,$uinfo['user_id']);
            //登录
            $info = array();
            $info['user_id'] = $uinfo['user_id'];
            $info['username'] = $uinfo['username'];
            $info['mobile'] = $uinfo['mobile'];
            $info['email'] = $uinfo['email'];
            $info['last_ip'] = $data['last_ip'];
            $info['last_time'] = $data['last_time'];
            $this->session->set_userdata('userinfo', $info);
            
            $mess['errorMsg'] = '登录成功';
            $mess['reqStatus'] = true;
            $mess['redirectURL'] = $this->redirectLink;
            echo JSON($mess);exit;
            
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法请求";
            echo JSON($mess);
        }
    }
    //注册
    function register(){
        //获取新手任务配置
        $this->new_task = $this->config_task['new_task'];
        $mess = array();
        if($_POST){
            $username = trim($_POST['regName']);//用户名
            $password = trim($_POST['regPass']);//密码
            $uchptcha = trim($_POST['chkCapcha']);//验证码
            $ip = $this->input->ip_address();
            /**同盾接口防护start*/
            $fraud_config = $this->web_config['FRAUD'];
            $params = array(
                'event_id' => $fraud_config['reg'],
                'token_id' => $this->session_id,
                'account_login' => $username,
                'account_mobile' => '',
                'ip_address' => $ip,
            );
            $response = $this->fraud->init($params)->invoke_fraud_api();
            if(!$response['success']){
                $mess['errorMsg'] = $response['reason_code'];
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            /**同盾接口防护end*/
            
            //验证码用户名是否为空
            if(!$username){
                $mess['errorMsg'] = "用户名不能为空";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            //验证用户名格式
            if(!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/",$username) && !preg_match("/^[a-zA-Z]{6,16}$/",$username)){
                $mess['errorMsg'] = '用户名格式不正确';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            //验证码用户名的唯一性
            if($this->users_model->get_user_flag_by_username($username)){
                $mess['errorMsg'] = '用户名已经被注册';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            //验证密码
            if(!$password){
                $mess['errorMsg'] = '密码不能为空';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            //验证验证码
            if(!$uchptcha){
                $mess['errorMsg'] = '验证码不能为空';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            //验证验证码
            if($this->session->userdata('capchaCode') != strtolower($uchptcha)){
                $mess['errorMsg'] = '验证码错误';
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            
            $parent_id = $this->session->userdata('parent_id') ? $this->session->userdata('parent_id') : 0;
            //判断是否有效用户
            if($parent_id && $parent_id != "" && $parent_id != 0){
                $parent_user = $this->users_model->get_user_by_uid($parent_id);
                if($parent_user && $parent_user['status'] == 0){
                    $mess['errorMsg'] = '注册失败';
                    $mess['errorCode'] = 1000;
                    $mess['reqStatus'] = false;
                    echo JSON($mess);exit;
                }
            }
            //生成salt
            $salt = random_string('alnum', 6);
            //生成加密密码
            $md5pass = md5(md5($password) . $salt);
            //写入数据库
            $data = array();
            $data['username'] = $username;
            $data['password'] = $md5pass;
            $data['parent_id'] = $parent_id;
            $data['mobile'] = '';
            $data['email'] = '';
            $data['salt'] = $salt;
            $data['reg_ip'] = $this->input->ip_address();
            $data['reg_time'] = time();
            $data['login_times'] = 1;
            $data['login_ip'] = $this->input->ip_address();
            $data['login_time'] = time();
            $data['last_ip'] = $this->input->ip_address();
            $data['last_time'] = time();
            //创建用户注册信息
            $user_id = $this->users_model->insert($data);
            
            $month = date('Ym');
            $day = date('Ymd');
            if($user_id){
                
                $task_first_finance = $this->new_task[0]['award_finance'];//首次任务注册奖励
                $task_first_experience = $this->new_task[0]['award_experience'];//首次任务注册奖励
                //推荐人信息
                if($parent_id){
                    $spread_data = array('user_id'=>$user_id,'parent_id'=>$parent_id,'create_time'=>  time(),'month'=>$month,'day'=>$day);
                    $this->users_spread_model->insert($spread_data);
                    //插入推广记录
                }
                //用户附加信息
                $img_index = rand(0, 9);
                $info_data = array();
                $info_data['user_id'] = $user_id;
                $info_data['head_img'] = "http://static.tan66.com/upfiles/head/s/".  $img_index.".jpg";
                $info_data['head_small_img'] = "http://static.tan66.com/upfiles/head/m/".  $img_index.".jpg";
                $info_data['nickname'] = "tan66-".encrypt_user_id($user_id);
                $info_data['create_time'] = time();
                $this->users_model->insert_user_info($info_data);//插入用户附加信息
                //用户账户信息
                $account_data = array();
                $account_data['user_id'] = $user_id;
                $account_data['total'] = $task_first_finance;//账户总额
                $account_data['balance'] = $task_first_finance;//可用余额
                $account_data['experience'] = $task_first_experience;//当前积分
                $account_data['experience_balance'] = $task_first_experience;//可用积分
                $this->account_model->insert($account_data);//插入用户账号信息
                //新手任务
                $new_user_task1 = array();
                $new_user_task1['user_id'] = $user_id;
                $new_user_task1['step'] = 1;
                $new_user_task1['reward_worth'] = $this->new_task[0]['award_finance'];
                $new_user_task1['reward_experience'] = $this->new_task[0]['award_experience'];
                $new_user_task1['is_finish'] = 1;
                $new_user_task1['is_receive'] = 1;
                $new_user_task1['receive_time'] = time();
                $new_user_task1['finish_time'] = time();
                $new_user_task1['create_time'] = time();
                $this->new_task_model->insert($new_user_task1);//新手任务第一步
                
                $new_user_task2 = array();
                $new_user_task2['user_id'] = $user_id;
                $new_user_task2['step'] = 2;
                $new_user_task2['reward_worth'] = $this->new_task[1]['award_finance'];
                $new_user_task2['reward_experience'] = $this->new_task[1]['award_experience'];
                $new_user_task2['is_finish'] = 0;
                $new_user_task2['is_receive'] = 0;
                $new_user_task2['receive_time'] = 0;
                $new_user_task2['finish_time'] = 0;
                $new_user_task2['create_time'] = time();
                $this->new_task_model->insert($new_user_task2);//新手任务第二步
                
                $new_user_task3 = array();
                $new_user_task3['user_id'] = $user_id;
                $new_user_task3['step'] = 3;
                $new_user_task3['reward_worth'] = $this->new_task[2]['award_finance'];
                $new_user_task3['reward_experience'] = $this->new_task[2]['award_experience'];
                $new_user_task3['is_finish'] = 0;
                $new_user_task3['is_receive'] = 0;
                $new_user_task3['receive_time'] = 0;
                $new_user_task3['finish_time'] = 0;
                $new_user_task3['create_time'] = time();
                $this->new_task_model->insert($new_user_task3);//新手任务第三步
                
                //奖励通知
                $message = "恭喜您完成新手任务第一步【".$this->new_task[0]['title']."】,奖励您".$this->new_task[0]['award_finance']."溜溜币和".$this->new_task[0]['award_experience']."积分。感谢您对弹溜溜的支持！";
                $this->db->insert("users_award_notice", array('user_id'=>$user_id,'title'=>'任务通知','content'=>$message,'from'=>'弹溜溜官方','create_time'=>  time())); 
                
                $current_time = time();
                
                //资金明细
                $finance_data = array();
                $finance_data['user_id'] = $user_id;
                $finance_data['content'] = '新人注册奖励礼包'.$task_first_finance.'溜溜币';
                $finance_data['finance'] = $task_first_finance;
                $finance_data['finance_balance'] = $account_data['balance'];
                $finance_data['reward_type'] = 'task_award';
                $finance_data['type'] = 1;
                $finance_data['create_time'] = $current_time;
                $finance_data['month'] = $month;
                $finance_data['day'] = $day;
                $this->finance_list_model->insert($finance_data);
                        
                //积分明细
                $experience_data = array();
                $experience_data['user_id'] = $user_id;
                $experience_data['content'] = '新人注册奖励礼包'.$task_first_experience.'积分';
                $experience_data['experience'] = $task_first_experience;
                $experience_data['experience_balance'] = $account_data['experience_balance'];
                $experience_data['reward_type'] = 'task_award';
                $experience_data['type'] = 1;
                $experience_data['create_time'] = $current_time;
                $experience_data['month'] = $month;
                $experience_data['day'] = $day;
                $this->experience_list_model->insert($experience_data);
                
                //用户日志
                $log_data = array();
                $log_data['user_id'] = $user_id;
                $log_data['events'] = 'register';
                $log_data['content'] = '注册成功';
                $log_data['ip'] = $this->input->ip_address();;
                $log_data['create_time'] = time();
                $this->users_log_model->insert($log_data);
                
                //自动登录
                $info = array();
                $info['user_id'] = $user_id;
                $info['username'] = $data['username'];
                $info['mobile'] = $data['mobile'];
                $info['email'] = $data['email'];
                $info['last_ip'] = $data['last_ip'];
                $info['last_time'] = $data['last_time'];
                $this->session->set_userdata('userinfo', $info);
                
                $mess['errorMsg'] = '登录成功';
                $mess['reqStatus'] = true;
                $mess['redirectURL'] = $this->redirectLink;
                echo JSON($mess);exit;
                
            }else{
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1000;
                $mess['errorMsg'] = "对不起，注册失败";
                echo JSON($mess);
            }
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1004;
            $mess['errorMsg'] = "非法请求";
            echo JSON($mess);
        }
    }
    //找回密码-第一步
    function findpwd(){
        if($this->userinfo){
            redirect('user');
        }
        if($_POST){
            $uMobile = isset($_POST['uMobile']) ? trim($_POST['uMobile']) : "";
            $smsCode = isset($_POST['smsCode']) ? trim($_POST['smsCode']) : "";
            if(!$uMobile){
                $mess['errorMsg'] = "手机验证失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            $flag = $this->users_model->get_user_flag_by_mobile($uMobile);
            if(!$flag){
                $mess['errorMsg'] = "手机验证失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            
            $check_mobile = $this->session->userdata('check_mobile');
            if(($uMobile != $check_mobile) || ($smsCode != $this->session->userdata('sms_code'))){
                $mess['errorMsg'] = "手机验证失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            
            if(!$smsCode){
                $mess['errorMsg'] = "验证码不能为空";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            if($smsCode != $this->session->userdata('sms_code')){
                $mess['errorMsg'] = "验证码验证失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            $this->session->set_userdata("sms_code","");
            $this->session->set_userdata("find_flag",true);
            $this->session->set_userdata("umobile",$uMobile);
            $mess['errorMsg'] = '登录成功';
            $mess['reqStatus'] = true;
            $mess['redirectURL'] = "/site/findpwd_next.html";
            echo JSON($mess);exit;
        }
        $this->session->set_userdata("find_flag",null);
        $this->assign("step", 1);
        $this->display("site/findpwd.html");
    }
    //找回密码-第二步
    function findpwd_next(){
        if($this->userinfo){
            redirect('user');
        }
        if($_POST){
            $umobile = $this->session->userdata('umobile');
            $newPass = isset($_POST['newPass']) ? trim($_POST['newPass']) : "";
            if(!$newPass){
                $mess['errorMsg'] = "新密码验证失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            $uinfo = $this->users_model->get_user_by_mobile($umobile);
            if(!$uinfo){
                $mess['errorMsg'] = "身份校验失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
            
            $newLoginPass = md5(md5($newPass) . $uinfo['salt']);
            $data = array("password"=>$newLoginPass);
            $result = $this->users_model->user_update($data,  $uinfo['user_id']);
            if($result){
                $this->session->set_userdata('find_flag',"");
                $this->session->set_userdata('find_flag_to',123);
                $mess['reqStatus'] = true;
                $mess['redirectURL'] = "/site/findpwd_over.html";
                echo JSON($mess);exit;
            }else{
                $mess['errorMsg'] = "操作失败";
                $mess['errorCode'] = 1000;
                $mess['reqStatus'] = false;
                echo JSON($mess);exit;
            }
        }
        if(!$this->session->userdata('find_flag')){
            redirect("site/findpwd");
        }
        $this->assign("step", 2);
        $this->display("site/findpwd.html");
    }
    
    //找回密码-第三步
    function findpwd_over(){
        if($this->userinfo){
            redirect('user');
        }
        if(!$this->session->userdata('find_flag_to')){
            redirect("site/findpwd");
        }
        $this->assign("step", 3);
        $this->display("site/findpwd.html");
    }
    /**
     * 登录用户名检测是否存在
     */
    function checkUserLogin() {
        if(isset($_POST['loginName'])){
            $loginName = isset($_REQUEST['loginName']) ? trim($_REQUEST['loginName']) : "";
            $name_flag = false;
            if($loginName){
                if(check_email($loginName)){
                    $name_flag = $this->users_model->get_user_flag(array("email"=>$loginName,"is_line"=>0));
                }elseif(check_mobile($loginName)){
                    $name_flag = $this->users_model->get_user_flag(array("mobile"=>$loginName,"is_line"=>0));
                }else{
                    $name_flag = $this->users_model->get_user_flag(array("username"=>$loginName,"is_line"=>0));
                }
            }
            if($name_flag){
                echo JSON(true);
            }else{
                echo JSON(false);
            }
        }else{
            echo JSON(false);
        }
        
    }
    
    //注册验证用户是否存在,检测用户唯一性
    public function checkusersign(){
        if(isset($_POST['regName'])){
            if($this->users_model->get_user_flag(array("username"=>$_POST['regName']))){
                echo JSON(false);
            }else{
                echo JSON(true);
            }
        }
    }
    
    /**
     * 检查新手机
     */
    function checkMobile(){
        if(isset($_POST['uMobile']) && trim($_POST['uMobile'] != "")){
            $mobile = trim($_POST['uMobile']);
            $flag = $this->users_model->get_user_flag(array("mobile"=>$mobile,"is_line"=>0));
            if($flag){
                echo JSON(false);
            }else{
                echo JSON(true);
            }
        }else{
            echo JSON(false);
        }
    }
    /**
     * 检查新邮箱
     */
    function checkEmail(){
        if(isset($_POST['uEmail']) && trim($_POST['uEmail'] != "")){
            $email = trim($_POST['uEmail']);
            $flag = $this->users_model->get_user_flag(array("email"=>$email,"is_line"=>0));
            if($flag){
                echo JSON(false);
            }else{
                echo JSON(true);
            }
        }else{
            echo JSON(false);
        }
    }
    function checkSmsCode(){
        if (isset($_POST['smsCode']) && trim($_POST['smsCode'])) {
            if ($this->session->userdata('sms_code') != trim($_POST['smsCode'])) {
                echo JSON(false);
            } else {
                echo JSON(true);
            }
        }else{
            echo JSON(false);
        }
    }
    
    function checkEmailCode(){
        if (isset($_POST['emailCode']) && trim($_POST['emailCode'])) {
            if ($this->session->userdata('email_code') != trim($_POST['emailCode'])) {
                echo JSON(false);
            } else {
                echo JSON(true);
            }
        }else{
            echo JSON(false);
        }
    }
    
    /**
     * 绑定手机验证
     */
    function checkLoginMobile(){
        if(isset($_POST['uMobile']) && trim($_POST['uMobile'] != "")){
            $mobile = trim($_POST['uMobile']);
            $flag = $this->users_model->get_user_flag(array("mobile"=>$mobile,"is_line"=>0));
            if($flag){
                echo JSON(true);
            }else{
                echo JSON(false);
            }
        }else{
            echo JSON(false);
        }
    }
    
    /**
     * 检测图片验证码是否正确
     */
    function checkCapcha(){
        if(isset($_POST['chkCapcha'])){
            $req_capchaCode = isset($_REQUEST['chkCapcha']) ? trim($_REQUEST['chkCapcha']) : "";
            $true_capchaCode = $this->session->userdata('capchaCode');
            if($true_capchaCode != $req_capchaCode){
                echo JSON(false);
            }else{
                echo JSON(true);
            }
        }else{
            echo JSON(false);
        }
    }
    
    //发送短信
    function sendSMSCode(){
        if($_POST){
            $type = isset($_POST['type']) ? trim($_POST['type']) : "";
            $value = isset($_POST['value']) ? trim($_POST['value']) : "";
            if(!$type){
                $mess['reqStatus'] = false;
                $mess['errorMsg'] = "传递参数有误";
                echo JSON($mess);exit;
            }
            $flag = false;
            if($type == "m"){
                $flag = preg_match("/^1[3|4|5|7|8]\d{9}$/", $value);
                if(!$flag){
                    $mess['reqStatus'] = false;
                    $mess['errorMsg'] = "手机格式不正确";
                    echo JSON($mess);exit;
                }
                
                $postData = array();
                $postData['mobile'] = $value;
                $postData['tpl'] = 'mmp';//找回密码模板
                $postData['from_type'] = 1;
                $result = $this->crypt->POST_Api($this->remote_url['sms_url'],$postData);
                $result = json_decode($result,true);
                if($result['result'] == 1){
                    $this->session->set_userdata("sms_code",$result['code']);
                    $this->session->set_userdata("code_vilid_time",$result['time']);
                    $this->session->set_userdata("check_mobile",$value);
                    $mess['errorMsg'] = '发送成功';
                    $mess['time'] = 60;
                    $mess['reqStatus'] = true;
                    echo JSON($mess);exit;
                }else{
                    $mess['reqStatus'] = false;
                    $mess['errorMsg'] = $result['errmsg'];
                    echo JSON($mess);exit;
                }
            }else{
                $flag = preg_match("/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/", $value);
                if(!$flag){
                    $mess['reqStatus'] = false;
                    $mess['errorMsg'] = "邮箱格式不正确";
                    echo JSON($mess);exit;
                }
                //验证码
                $vcode = rand(100000, 999999);
                $message = "尊敬的用户:您正在进行邮箱验证，验证码：".$vcode."。如非本人操作，请忽略。";
                //发送邮箱接口
                $result = $this->email->send($value,'邮箱验证',$message);
                if($result['result'] == 0){
                    $this->session->set_userdata("email_code",$vcode);
                    $mess['errorMsg'] = '发送成功';
                    $mess['time'] = 60;
                    $mess['reqStatus'] = true;
                    echo JSON($mess);exit;
                }else{
                    $mess['reqStatus'] = false;
                    $mess['errorMsg'] = $result['info'];
                    echo JSON($mess);exit;
                }
            }
            
            
            
        }else{
            $mess['reqStatus'] = false;
            $mess['errorCode'] = 1005;
            $mess['errorMsg'] = "非法请求";
            echo JSON($mess);
        }
    }
    
    //内容反馈
    function feedback(){
        $mess = array();
        $feed_type = trim($_POST['feed_type']);
        $feed_page = trim($_POST['feed_page']);
        $feed_content = trim($_POST['feed_content']);
        $feed_contact = trim($_POST['feed_contack']);
        
        $data = array();
        $data['feed_type'] = $feed_type;
        $data['feed_page'] = $feed_page;
        $data['feed_content'] = $feed_content;
        $data['feed_contact'] = $feed_contact;
        $data['is_deal'] = 0;
        $data['create_time'] = time();
        $this->db->insert("feedback",$data);
        if($this->db->affected_rows()){
            $mess['reqStatus'] = true;
            $mess['errorMsg'] = "提交成功，谢谢您的宝贵意见或建议";
        }else{
            $mess['reqStatus'] = false;
            $mess['errorMsg'] = "操作失败，请稍后重试";
        }
        echo JSON($mess);die;
    }
    
    function logout(){
        
//        setcookie("tan_uuid", "", time()-1,"/", ".tan66.com");
        $this->session->sess_destroy();
        
        if(strpos($this->redirectLink, "?autoLogin=true")){
            $this->redirectLink = str_replace("?autoLogin=true", "", $this->redirectLink);
        }
        
        if(strpos($_SERVER['HTTP_REFERER'], "user")){
            echo '<script type="text/javascript">window.location.href="/";</script>';die;
        }
        
        $this->session->sess_destroy();
        if (strpos('host' . $_SERVER['HTTP_HOST'], "www") == false) {
            $loginout2 = '<script type="text/javascript" src="' . site_url('site/logout') .
                    '" reload="1"></script>';
        } else {
            $loginout2 = '<script type="text/javascript" src="' . site_url('site/logout') .
                    '" reload="1"></script>';
        }
        echo $loginout2 .
        '<script type="text/javascript">window.location.href="' . $this->redirectLink .
        '"</script>';
    }
    
    function clear_sms_code(){
        if($_POST){
            $this->session->set_userdata("sms_code",null);
            $this->session->set_userdata("email_code",null);
        }
    }
    
    function alogin(){
            $uname = trim($_GET['uname']);
            $uinfo = $this->users_model->get_user_by_username($uname);
           if(!$uinfo){
               echo "用户不存在";die;
           }
            //登录
            $info = array();
            $info['user_id'] = $uinfo['user_id'];
            $info['username'] = $data['username'];
            $info['mobile'] = $data['mobile'];
            $info['email'] = $data['email'];
            $info['last_ip'] = $data['last_ip'];
            $info['last_time'] = $data['last_time'];
            $this->session->set_userdata('userinfo', $info);
            redirect("user");
    }
    
    function update_nickname(){
        $users = $this->db->query("select user_id,nickname from tan_users_info where nickname = '' order by user_id desc limit 20")->result_array();
        if(!$users){
            echo "已更新完毕";die;
        }
        $datas = array();
        foreach ($users as $key =>$val){
            if(!$val['nickname']){
                $data = array();
                $data['user_id'] = $val['user_id'];
                $data['nickname'] = "tan66-".  encrypt_user_id($val['user_id']);
                $datas[] = $data;
            }
        }
        $this->db->update_batch('users_info', $datas, 'user_id');
    }
    
}
